package com.pl.service.impl;

import com.alibaba.excel.EasyExcel;
import com.pl.entity.DeviceLog;
import com.pl.dao.DeviceLogDao;
import com.pl.entity.SysUser;
import com.pl.lister.DeviceLogReadLister;
import com.pl.service.DeviceLogService;
import com.pl.utils.R;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * (DeviceLog)表服务实现类
 *
 * @author makejava
 * @since 2024-04-30 11:13:03
 */
@Service("deviceLogService")
public class DeviceLogServiceImpl implements DeviceLogService {
    @Autowired
    private DeviceLogDao deviceLogDao;

    /**
     * 通过ID查询单条数据
     *
     * @param logId 主键
     * @return 实例对象
     */
    @Override
    public DeviceLog queryById(Integer logId) {
        return this.deviceLogDao.queryById(logId);
    }

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @Override
    public List<DeviceLog> queryByPage(Map<String, Object> map) {
        return this.deviceLogDao.queryAllByLimit(map);
    }

    /**
     * 新增数据
     *
     * @param deviceLog 实例对象
     * @return 实例对象
     */
    @Override
    public boolean insert(DeviceLog deviceLog, HttpSession session) {
        SysUser user = (SysUser) session.getAttribute("user");
        deviceLog.setLogCreateby(user.getUserName());
        deviceLog.setLogCreatetime(new Date());
        int insert = deviceLogDao.insert(deviceLog);
        if (insert > 0) {
            return true;
        }
        return false;
    }

    /**
     * 修改数据
     *
     * @param deviceLog 实例对象
     * @return 实例对象
     */
    @Override
    public DeviceLog update(DeviceLog deviceLog) {
        this.deviceLogDao.update(deviceLog);
        return this.queryById(deviceLog.getLogId());
    }

    /**
     * 通过主键删除数据
     *
     * @param logId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer logId) {
        return this.deviceLogDao.deleteById(logId) > 0;
    }

    /**
     * 批量删除
     *
     * @param list
     * @return
     */
    @Override
    public boolean deleteBatch(List<Integer> list) {
        int num = deviceLogDao.deleteBatch(list);
        if (num > 0) {
            return true;
        }

        return false;

    }

    @Override
    public boolean insertBatch(List<DeviceLog> list) {
        int i = deviceLogDao.insertBatch(list);
        if (i > 0) {
            return true;
        }
        return false;
    }

    @Override
    public List<DeviceLog> findAll() {
        return deviceLogDao.findAll();
    }

    @Override
    public String importFile(MultipartFile file) {

        try {
            EasyExcel.read(file.getInputStream(),DeviceLog.class,new DeviceLogReadLister(this)).sheet("日志信息").doReadSync();
            return "import_success";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "fail";
    }

    @Override
    public void exportFile(HttpServletResponse response) {
        List<DeviceLog>list=deviceLogDao.findAll();
        try {
            EasyExcel.write(response.getOutputStream(),DeviceLog.class).sheet("日志信息").doWrite(list);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }



}
